/**
 * ------------------------表单校验------------------
 */
//正整数
function checkPInt(field) {
    var g = /^[1-9]*[1-9][0-9]*$/;
    $("#"+field.id).attr("style","border-color: ;");
	$("#"+field.id).attr("title","");
	if(!g.test($(field).val())){
		$("#"+field.id).attr("style","border-color:red;");
		$("#"+field.id).attr("title","必须为正整数");
	}else{
		$("#"+field.id).attr("style","border-color:;");
		$("#"+field.id).attr("title","");
	}
}
/**
 * 数字验证
 */
function checkNumber(field){
	$(field).attr("style","border-color: ;");
	$(field).attr("title","");
	if(field.value==''||isNaN(field.value)){
		$(field).attr("style","border-color:red;");
		$(field).attr("title","必须为数字");
		return false;
	}else{
		$(field).attr("style","border-color:;");
		$(field).attr("title","");
		return true;
	}
}
/**
 * 非空验证
 * @param field 表单元素
 */
function checkNotNull(field){
	$(field).attr("style","border-color: ;");
	$(field).attr("title","");
	if(""==field.value){
		$(field).attr("style","border-color:red;");
		$(field).attr("title","不能为空");
		return false;
	}else{
		$(field).attr("style","border-color:;");
		$(field).attr("title","");
		return true;
	}
}
/**
 * 编码验证,可以为空
 * @param field 表单元素
 */
function checkCode(field){
	$("#"+field.id).attr("style","border-color: ;");
	$("#"+field.id).attr("title","");
	if(""==field.value){
	}else{
		var reg=/^[\w_\s]+$/;
		if(!reg.test($.trim(field.value))){
			$("#"+field.id).attr("style","border-color:red;");
			$("#"+field.id).attr("title","编码必须为数字字母下划线！");
		}else{
			$("#"+field.id).attr("style","border-color:;");
			$("#"+field.id).attr("title","");
		}
	}
}
/**
 * 编码验证,不能为空
 * @param field 表单元素
 */
function checkCodeNotNull(field){
	if(""==field.value){
		$("#"+field.id).attr("style","border-color:red ;");
		$("#"+field.id).attr("title","编码不能为空");
	}else{
		var reg=/^[\w_\s]+$/;
		if(!reg.test($.trim(field.value))){
			$("#"+field.id).attr("style","border-color:red;");
			$("#"+field.id).attr("title","编码必须为数字字母下划线！");
		}else{
			$("#"+field.id).attr("style","border-color:;");
			$("#"+field.id).attr("title","");
		}
	}
}
/**
 * 校验手机号,电话号码
 * @param field 表单元素
 */
function checkPhoneNull(field){
	$("#"+field.id).attr("style","border-color: ;");
	$("#"+field.id).attr("title","");
	if(""==field.value){
		$("#"+field.id).attr("style","border-color:red;");
		$("#"+field.id).attr("title","不能为空");
	}else{
		var reg = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
		if(!reg.test($.trim(field.value))){
			$("#"+field.id).attr("style","border-color:red;");
			$("#"+field.id).attr("title","电话格式不正确！");
		}else{
			$("#"+field.id).attr("style","border-color:;");
			$("#"+field.id).attr("title","");
		}
	}
}

/**
 * 编码验证,且不为空
 * @param field 表单元素
 */
function checkCodeNotNull(field){
	$("#"+field.id).attr("style","border-color: ;");
	$("#"+field.id).attr("title","");
	if(""==field.value){
		$("#"+field.id).attr("style","border-color:red;");
		$("#"+field.id).attr("title","不能为空");
	}else{
		var reg=/^[\w_\s]+$/;
		if(!reg.test($.trim(field.value))){
			$("#"+field.id).attr("style","border-color:red;");
			$("#"+field.id).attr("title","编码必须为数字字母下划线！");
		}else{
			$("#"+field.id).attr("style","border-color:;");
			$("#"+field.id).attr("title","");
		}
	}
}
//判断ip地址的合法性，可以为空
function checkIP(field){
	$("#"+field.id).attr("style","border-color: ;");
	$("#"+field.id).attr("title","");
	if(""==field.value){
	}else{
	    var reg=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
		if(!reg.test($.trim(field.value))){
			$("#"+field.id).attr("style","border-color:red;");
			$("#"+field.id).attr("title","IP地址格式错误");
		}else{
			$("#"+field.id).attr("style","border-color:;");
			$("#"+field.id).attr("title","");
		}
	}
}
//判断ip地址的合法性
function checkIPNotNull(field){
	$(field).attr("style","border-color: ;");
	$(field).attr("title","");
	if(""==field.value){
		$(field).attr("style","border-color:red;");
		$(field).attr("title","不能为空");
	}else{
	    var reg=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
		if(!reg.test($.trim(field.value))){
			$(field).attr("style","border-color:red;");
			$(field).attr("title","IP地址格式错误");
		}else{
			$(field).attr("style","border-color:;");
			$(field).attr("title","");
		}
	}
}
/**
 * 
 * @param field 表单元素
 * @param name 属性名称
 * @param ismust 是否必填
 * @param reg 正则表达式
 * @param msg 提示信息
 * @param maxlength 最大长度
 */
function checkReg(field,name,ismust,reg,msg,maxlength){
	if(""==msg||null==msg){
		msg="数据不合法!";
	}
	if(ismust){
		if($.trim(field.value)==""){
			$("#"+field.id).attr("style","border-color:red;");
			$("#"+field.id).attr("title",name+"不能为空！");
		}else{
			$("#"+field.id).attr("style","border-color:;");
			$("#"+field.id).removeAttr("title");
			if(""!=reg&&null!=reg){
				if(!reg.test($.trim(field.value))){
					$("#"+field.id).attr("style","border-color:red;");
					$("#"+field.id).attr("title",""+name+":"+msg);
				}else{
					if(field.value.length>maxlength){
						$("#"+field.id).attr("style","border-color:red;");
						$("#"+field.id).attr("title","输入长度过长，请调整！");
					}else{
						$("#"+field.id).attr("style","border-color:;");
						$("#"+field.id).removeAttr("title");
					}
				}
			}
			
		}
	}else{
		if(""==$.trim(field.value)){
			
		}else{
			$("#"+field.id).attr("style","border-color:;");
			$("#"+field.id).removeAttr("title");
			if(""!=reg){
				if(!reg.test($.trim(field.value))){
					$("#"+field.id).attr("style","border-color:red;");
					$("#"+field.id).attr("title",""+name+":"+msg);
				}else{
					if(field.value.length>maxlength){
						$("#"+field.id).attr("style","border-color:red;");
						$("#"+field.id).attr("title","输入长度过长，请调整！");
					}else{
						$("#"+field.id).attr("style","border-color:;");
						$("#"+field.id).removeAttr("title");
					}
				}
			}
		}
	}
}


/**
 *@desc 判断输入是否包含空格
 *@return 
 *	true-含有空格   
 *	false-不包含 
 */
var $isSpace = function(name){
	var re = /\s/;	
	return re.test(name);
} 
/**
 * 长度校验（支持input和textarea）其他可自行添加
 * @param obj 元素
 * @param maxlength 最大长度
 */
function checkLength(obj,maxlength){
	if($(obj).attr("style")=='border-color:red;'){
		return;
	}
	var value='';
	if($(obj)[0].nodeName=='INPUT'){
		value = $(obj).val();
	}else if($(obj)[0].nodeName=='TEXTAREA'){
		value=$(obj).text();
	}
	if(value.length>maxlength){
		$("#"+obj.id).attr("style","border-color:red;");
		$("#"+obj.id).attr("title","输入长度过长，请调整！");
		return false;
	}else{
		$("#"+obj.id).attr("style","border-color:;");
		$("#"+obj.id).removeAttr("title");
		return true;
	}
}